﻿/*
 * QWine Queue Machine (Special build for Immigration Office)
 * Copyright (C) 2010 Wish Enterprise Development Team

 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 * 
 * Contact Us:
 * E-Mail   : contact@wishenterprise.com     Website: http://www.wishenterprise.com
 * 
 * */

using System;
using System.Windows.Forms;
using SmsEntity.Local;
using SmsRepository;
using SmsRepository.Local;

namespace SmsManualRespond
{
    /// <summary>
    /// Logon UI
    /// </summary>
    public partial class LogonPresentation : Form
    {
        private User _user;

        /// <summary>
        /// Initializes a new instance of the <see cref="LogonPresentation"/> class.
        /// </summary>
        public LogonPresentation()
        {
            InitializeComponent();
        }

        /// <summary>
        /// Gets or sets the user.
        /// </summary>
        /// <value>The user.</value>
        public User User
        {
            get { return _user; }
            set { _user = value; }
        }

        /// <summary>
        /// Sets the user.
        /// </summary>
        /// <param name="usr">The usr.</param>
        public void SetUser(string usr)
        {
            txtUserId.Text = usr;
            txtUserId.Enabled = false;
        }

        /// <summary>
        /// Handles the Validated event of the txtUserId control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void txtUserId_Validated(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(txtUserId.Text.Trim())) return;

            IDataAccess<User> db = new UserBase();
            _user = db.GetById(txtUserId.Text.Trim());
        }

        /// <summary>
        /// Handles the Click event of the btnLogin control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void btnLogin_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(txtUserId.Text.Trim()) |
                (_user == null) |
                string.IsNullOrEmpty(txtPassword.Text.Trim())) return;


            string usr = txtUserId.Text.Trim();

            string pwd =
                UserCryptoService.Encrypt(usr, txtPassword.Text.Trim());

            var db = new UserBase();
            if (!db.AuthenticateUser(usr, pwd))
            {
                MessageBox.Show(@"Access Denied!", @"Warning!",
                                MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }

            DialogResult = DialogResult.OK;
            Close();
        }

        /// <summary>
        /// Handles the Click event of the btnCancel control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void btnCancel_Click(object sender, EventArgs e)
        {
            DialogResult = DialogResult.Cancel;
            Close();
        }

        /// <summary>
        /// Handles the TextChanged event of the txtUserId control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void txtUserId_TextChanged(object sender, EventArgs e)
        {
            ((TextBox) sender).Text = EnvironmentRepository.SuppressChar(((TextBox) sender).Text);
        }

        /// <summary>
        /// Handles the TextChanged event of the txtPassword control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void txtPassword_TextChanged(object sender, EventArgs e)
        {
            ((TextBox) sender).Text = EnvironmentRepository.SuppressChar(((TextBox) sender).Text);
        }
    }
}